//
//  ChildPlayListViewController.m
//  XMLY
//
//  Created by lanou on 15/11/5.
//  Copyright © 2015年 曾杰. All rights reserved.
//

#import "ChildPlayListViewController.h"
#import "PKRequestManager.h"
#import "ChildListCell.h"
#import "ChildListHeaderModel.h"
#import "ChildListModel.h"
#import "MJRefresh.h"
#import "ChildMainPlayViewController.h"
#import "playListView.h"
@interface ChildPlayListViewController ()
//@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArr;
@property (nonatomic, strong) ChildListHeaderModel *headerModel;
@property (nonatomic, strong)playListView * listView;
@end

@implementation ChildPlayListViewController
- (NSMutableArray *)dataSourceArr{
    if (_dataSourceArr == nil) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}
- (void)requestData{
    NSString *newUrlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%ld/true/%ld/20?albumId=%ld&isAsc=true&device=android&pageSize=20", (NSInteger)self.albumId, self.dataSourceArr.count / 20 + 1, (NSInteger)self.albumId];
    
    [PKRequestManager requestWithType:GET urlString:newUrlStr parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves) error:nil];
        for (NSDictionary *dic1 in dic[@"tracks"][@"list"]) {
            ChildListModel *model = [[ChildListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [self.dataSourceArr addObject:model];
        }
        
        self.headerModel = [[ChildListHeaderModel alloc] init];
        [_headerModel setValuesForKeysWithDictionary:dic[@"album"]];
        
        [self.tableView reloadData];
        
        
    } error:^(NSError *error) {
        NSLog(@"%@", [error localizedDescription]);
    }];
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self requestData];
    self.tableView.rowHeight = 80;
    [self.tableView registerClass:[ChildListCell class] forCellReuseIdentifier:@"listCell"];
    
    __weak typeof(self) weakSelf = self;
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf requestData];
        [self.tableView.header endRefreshing];
    }];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestData];
        [self.tableView.footer endRefreshing];
    }];
    
    _listView = [[playListView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/2-60)];
    self.tableView.tableHeaderView = _listView;

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChildListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listCell" forIndexPath:indexPath];
    ChildListModel *model = self.dataSourceArr[indexPath.row];
    [cell configureCellWithModel:model];
    // Configure the cell...
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ChildMainPlayViewController *childPlayVC = [[ChildMainPlayViewController alloc] init];
    childPlayVC.playListUrlArr = self.dataSourceArr;
    childPlayVC.headerModel = self.headerModel;
    childPlayVC.index = indexPath.row;
    [self presentViewController:childPlayVC animated:YES completion:nil];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
